package cn.sfa.service.customer;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.sfa.cache.CacheFactory;
import cn.sfa.cache.ICacheHelper;
import cn.sfa.common.pagination.Pagination;
import cn.sfa.common.utils.CommonUtil;
import cn.sfa.common.utils.MajorKeyGenerate;
import cn.sfa.common.utils.TableNameEnum;
import cn.sfa.entity.customer.SaleCustomerSee;
import cn.sfa.entity.customer.SaleHideCustomer;
/**
 * 
* 类名称：CustomerService   
* 类描述： 客户拜访相关服务类
* 创建人：LLY   
* 创建时间：2012-2-15 下午11:12:18    
* 修改备注：   
* @version
 */
@Service
@Transactional
public class CustomerSeeService {
	
	Logger logger = LoggerFactory.getLogger(CustomerSeeService.class);
	ICacheHelper  cacheHelper = CacheFactory.CreateCacheHelper();
	
	@Autowired
	NamedParameterJdbcTemplate jdbcTemplate;
	
	@Autowired
	MajorKeyGenerate majorKeyGenerate;
	/**
	 * 添加客户拜访
	 * @param customer
	 * @return
	 */
	public boolean addCustomerSee(SaleCustomerSee customersee)
	{
		String sql="insert into salecustomersee(SALECUSTOMERSEEID,CREATEDATE,CREATEUSER,UPDATEDATE,UPDATEUSER,"+				
		"CUSTOMERID,VISITDATE,VISITCONTENT,STATUS,ISTURN,VISITRESULT,VISITCOUNT) values(:SALECUSTOMERSEEID,:CREATEDATE,"+
				":CREATEUSER,:UPDATEDATE,:UPDATEUSER,:CUSTOMERID,:VISITDATE,:VISITCONTENT,:STATUS,"+
				":ISTURN,:VISITRESULT,(select count(*) from salecustomersee where customerid=:CUSTOMERID)+1)";
		Map<String,Object> paramMap=new HashMap<String,Object>();
		
		
		paramMap.put("SALECUSTOMERSEEID",majorKeyGenerate.keyGenerate(TableNameEnum.SALECUSTOMERSEE));
		paramMap.put("CREATEDATE",new Date(customersee.getCreateDate().getTime()));
		paramMap.put("CREATEUSER",CommonUtil.jugeAndFixNull(customersee.getCreateUid()));				
		paramMap.put("UPDATEDATE",new Date(customersee.getUpdateDate().getTime()));				
		paramMap.put("UPDATEUSER",CommonUtil.jugeAndFixNull(customersee.getUpdateId()));				
		paramMap.put("CUSTOMERID",CommonUtil.jugeAndFixNull(customersee.getCustomerId()));
	    paramMap.put("VISITDATE",(customersee.getVisitDate()));					
		paramMap.put("VISITCONTENT",CommonUtil.jugeAndFixNull(customersee.getVisitContent()));			
		paramMap.put("STATUS",CommonUtil.jugeAndFixNull(customersee.getStatus()));						
		paramMap.put("ISTURN",CommonUtil.jugeAndFixNull(customersee.getIsTurn()));						
		paramMap.put("VISITRESULT",CommonUtil.jugeAndFixNull(customersee.getVisitResult()));
		
		int count=jdbcTemplate.update(sql, paramMap);
		if(count>0)
			return true;
		return false;
	}
	
	/**
	 * 分页查询客户
	 * @param currentPage
	 * @param numPerPage
	 * @param url
	 * @param customer
	 * @return
	 */
	public Pagination searchCustomers(int currentPage,//页码
			int numPerPage,//页尺寸
			String url,//分页url
			String saleHideCustomerId,
			String customerClassify) {
		
		StringBuilder sqlStr = new StringBuilder();
		sqlStr.append("select * from table(pkg_sfa_customers.searchcustomers(:customerid,:customerClassify))");
		Map<String,Object> paramMap=new HashMap<String,Object>();
		paramMap.put("customerid", saleHideCustomerId);
		paramMap.put("customerClassify", customerClassify);
		  
				 
		return new Pagination(sqlStr.toString(),paramMap,currentPage,numPerPage,url,jdbcTemplate); 
	}
	
	/**
	 * 分页查询客户
	 * @param currentPage
	 * @param numPerPage
	 * @param url
	 * @param customer
	 * @return
	 */
	public Pagination searchFastCustomers(int currentPage,//页码
			int numPerPage,//页尺寸
			String url,//分页url
			String saleHideCustomerId) {
		
		StringBuilder sqlStr = new StringBuilder();
		sqlStr.append("select * from table(pkg_sfa_customers.searchFastcustomers(:customerid))");
		Map<String,Object> paramMap=new HashMap<String,Object>();
		paramMap.put("customerid", saleHideCustomerId);
		  
				 
		return new Pagination(sqlStr.toString(),paramMap,currentPage,numPerPage,url,jdbcTemplate); 
	}

	public boolean deleteCustomerById(String saleCustomerSeeId) {
		String sql="delete salecustomersee where salecustomerseeid=:saleCustomerSeeId";
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("saleCustomerSeeId", saleCustomerSeeId);
		int count=jdbcTemplate.update(sql, map);
		if(count>0)
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 根据客户ＩＤ查找客户
	 * @param saleHideCustomerId
	 * @return
	 */
	public SaleCustomerSee getCustomer(String saleCustomerSeeId) {
		String sql = "select * from table(pkg_sfa_customers.getCustomer(:salecustomerseeid)) ";
		final SaleCustomerSee customer=new SaleCustomerSee();
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("salecustomerseeid",saleCustomerSeeId);
		jdbcTemplate.query(sql,paramMap ,new RowCallbackHandler() {
					
					@Override
					public void processRow(ResultSet rs) throws SQLException {
						
						customer.setSaleCustomerSeeId(rs.getString("salecustomerseeid"));
						customer.setCreateDate(rs.getDate("createdate"));
						customer.setCreateUid(rs.getString("createuser"));
						customer.setUpdateDate(rs.getDate("updatedate"));
						customer.setUpdateId(rs.getString("updateuser"));
						customer.setCustomerId(rs.getString("customerid"));
						customer.setVisitDate(rs.getTimestamp("visitdate"));
						customer.setVisitContent(rs.getString("visitcontent"));
						customer.setVisitResult(rs.getString("visitresult"));
						customer.setStatus(rs.getString("status"));
					}
						
					});
		return customer;
	}
	
	/**
	 * 编辑客户信息
	 * @param customer
	 * @return
	 */
	public boolean updateCustomer(SaleCustomerSee customer) {
		String sql="update salecustomersee set visitcontent=:visitcontent,visitresult=:visitresult,status=:status"+
		" where salecustomerseeid=:salecustomerseeid";
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("visitcontent",CommonUtil.jugeAndFixNull(customer.getVisitContent()));
		paramMap.put("visitresult", CommonUtil.jugeAndFixNull(customer.getVisitResult()));
		paramMap.put("status",CommonUtil.jugeAndFixNull(customer.getStatus()));
		paramMap.put("salecustomerseeid",CommonUtil.jugeAndFixNull(customer.getSaleCustomerSeeId()));
		int count=jdbcTemplate.update(sql, paramMap);
		if(count>0)
			return true;
		return false;
	}
}
